﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MapCenterSystem.DataInfo;
using MapCenterSystem.DefineType;
using MapCenterSystem.Utility;
using MapCenterSytem.Process;
using MobiMap_Service.Models;
using MobiMap.Guest;
using Direction = MobiMap_Service.Models.Direction;
using MapHandler = MobiMap_Service.Models.MapHandler;

namespace MobiMap_Service.Controllers
{
    public class MapApiExController : ApiController
    {
        MapHandler mapHandler = new MapHandler();

        public Direction GetMapDirection(string streetInfo)
        {
            Direction direction = new Direction();

            double[] arrayCoordinate = Helper.getArrayDouble(streetInfo);
            Coordinate node1 = new Coordinate();
            node1.Latitude = arrayCoordinate[0];
            node1.Longitude = arrayCoordinate[1];
            Coordinate node2 = new Coordinate();
            node2.Latitude = arrayCoordinate[2];
            node2.Longitude = arrayCoordinate[3];

            //chi duong di
            direction = DirectionControl.BindListGraphNodeEx(node1, node2);

            // ve map
            Image img = mapHandler.DrawStreetInMap(node1, node2);
            direction.ArrayImage = Helper.imageToByteArray(img);

            return direction;
        }

        public NearPlace GetListServiceAroundPosition(string keyword, string strCoordinate, string strTop)
        {
            //keyword = "Tram ATM";
            int intTop = 10;
            if (strTop != "")
                try
                {
                    intTop = Int32.Parse(strTop);
                }catch (Exception){
                }
            double[] arr = Helper.getArrayDouble(strCoordinate);
            Coordinate coordinate = new Coordinate(arr[0], arr[1]);

            keyword = StringHelper.GetSearchString(keyword, AppDomain.CurrentDomain.BaseDirectory + ConfigurationManager.AppSettings["abbr_file"]);
            IList<PlaceInfo> lstPlaceInfo = new List<PlaceInfo>();
            PlaceTypeInfo type = PlaceTypeEx.GetPlaceTypeByDescription(keyword);
            if (type != null)
            {
                lstPlaceInfo = PlaceEx.GetPlaceByTypeAndDistance(type.ID,
                                new Coordinate(coordinate.Latitude, coordinate.Longitude), intTop);
            }

            List<PlaceInfo> lstService = (List<PlaceInfo>)lstPlaceInfo;
            Image img = mapHandler.ServiceAround(lstService, coordinate.Latitude, coordinate.Longitude);
            byte[] array = Helper.imageToByteArray(img);
            
            NearPlace nearPlace = new NearPlace();
            nearPlace.ListPlace = lstPlaceInfo;
            nearPlace.ArrayImage = array;
            return nearPlace;
        }
        
       
    }
}
